package com.pb.camera;

import java.util.Map;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.pb.camera.application.App;
import com.pb.camera.helper.JsonHelper;
import com.pb.camera.utils.Constants;
import com.pb.camera.utils.JsonAnalyle;
import com.pb.camera.utils.MyTextWatcher;
import com.pb.camera.utils.RegexValidateUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class SettingPasswordActivity extends Activity implements
		OnClickListener {
	private EditText edit_account;
	private EditText edit_email;
	private EditText edit_password;
	private EditText edit_password2;
	private ImageView image_back;
	private ImageView image_account;
	private ImageView image_email;
	private ImageView image_password;
	private ImageView image_password2;
	private LinearLayout linear_rootview;
	private CheckBox check;
	private boolean hasName = false;
	private View viewLog;
	private String mobile;
	private HttpUtils httpUtils = new HttpUtils();
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {

			if (msg.arg2 == 6) {
				Toast.makeText(SettingPasswordActivity.this, (String) msg.obj,
						Toast.LENGTH_SHORT);

			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.password_setting);
		if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}
		App.getInstance().getAllActivity().add(this);
		mobile = getIntent().getExtras().getString("mobile");
		initView();
	}

	private void initView() {
		final View set_password_linear = findViewById(R.id.set_password_linear);
		final int height = App.getInstance().getStatusBarHeight(this);
		set_password_linear.getViewTreeObserver().addOnPreDrawListener(
				new OnPreDrawListener() {
					@Override
					public boolean onPreDraw() {
						// TODO Auto-generated method stub
						// TODO Auto-generated method stub
						set_password_linear.setPadding(0, height, 0, 0);
						set_password_linear.getViewTreeObserver()
								.removeOnPreDrawListener(this);
						return true;
					}
				});
		linear_rootview = (LinearLayout) findViewById(R.id.password_line_rootview);
		linear_rootview.setOnClickListener(this);
		image_back = (ImageView) findViewById(R.id.password_image_back);
		image_back.setOnClickListener(this);
		viewLog = findViewById(R.id.password_line_next);
		edit_account = (EditText) findViewById(R.id.password_edit_account);
		edit_email = (EditText) findViewById(R.id.password_edit_email);
		edit_password = (EditText) findViewById(R.id.password_edit_password);
		edit_password2 = (EditText) findViewById(R.id.password_edit_password2);
		image_account = (ImageView) findViewById(R.id.password_image_reset_account);
		image_email = (ImageView) findViewById(R.id.password_image_reset_email);
		image_password = (ImageView) findViewById(R.id.password_image_reset_password);
		image_password2 = (ImageView) findViewById(R.id.password_image_reset_password2);
		edit_account.addTextChangedListener(new MyTextWatcher(edit_account,
				image_account));
		edit_email.addTextChangedListener(new MyTextWatcher(edit_email,
				image_email));
		edit_password.addTextChangedListener(new MyTextWatcher(edit_password,
				image_password));
		edit_password2.addTextChangedListener(new MyTextWatcher(edit_password2,
				image_password2));
		check = (CheckBox) findViewById(R.id.password_checkbox);
		edit_account.setFocusable(true);
		edit_account.setFocusable(true);
		// 失去焦点是检验接口的唯一性
		edit_account.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (!hasFocus) {
					if (!RegexValidateUtil.checkName(edit_account.getText()
							.toString())) {
						setToast("账户名不合要求");
					} else {// 检查账户的唯一性

						Log.i("===", "检查账号是否重复");
						getCode(Constants.checkAccountUnique(edit_account
								.getText().toString()),
								SettingPasswordActivity.this, 1);

					}

				}

			}
		});

		// 点击下一步的时候
		// 不是在通过onClick设置的
		viewLog.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!check.isChecked()) {
					setToast("请阅读并同意注册协议");
				} else if (!RegexValidateUtil.checkName(edit_account.getText()
						.toString())) {
					setToast("账户名不合要求");

				} else if (!RegexValidateUtil.checkEmail(edit_email.getText()
						.toString())) {
					setToast("邮件格式不对");
				} else if (!RegexValidateUtil.checkName(edit_password.getText()
						.toString())) {
					setToast("密码格式不正确");
				} else if (RegexValidateUtil.checkName(edit_password.getText()
						.toString())) {
					if (!edit_password.getText().toString()
							.equals(edit_password2.getText().toString())) {
						setToast("两次密码不一致");
					}

					else {
						// 避免用户名的重复
						if (hasName) {
							Intent intent = new Intent();
							intent.setClass(SettingPasswordActivity.this,
									CreateGesturePasswordActivity.class);
							Bundle bundle = new Bundle();
							bundle.putString("whereFrom", "setting");
							bundle.putString("account", edit_account.getText()
									.toString());
							bundle.putString("password", edit_password
									.getText().toString());
							bundle.putString("email", edit_email.getText()
									.toString());
							bundle.putString("mobile", mobile);
							intent.putExtras(bundle);
							startActivity(intent);
						} else {
							// setToast("用户名验证失败,请重新输入用户名");
							getCode(Constants.checkAccountUnique(edit_account
									.getText().toString()),
									SettingPasswordActivity.this, 1);
						}

					}
				}

			}
		});

	}

	private void setToast(String mess) {
		Toast.makeText(this, mess, Toast.LENGTH_SHORT).show();

	}

	private void getCode(String url, final Activity activity, final int mode) {
		httpUtils.send(HttpMethod.POST, url, new RequestCallBack<String>() {
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				Toast.makeText(activity, "网络访问失败,请检查你的网络", 0).show();
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// Map map=JsonHelper.jsonStringToMap(arg0.result,new
				// String[]{"errcode","errmsg"} , null);
				// 调用的原因是显示提示信息
				boolean flag = JsonAnalyle.jsonAnalyse(arg0.result,
						SettingPasswordActivity.this, null, handler);
				if (flag) {
					hasName = true;
				} else {
					hasName = false;
				}
			}
		});
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.password_line_rootview:
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
			break;
		case R.id.password_image_back:
			// 不可以调用的系统的返回键
			finish();
			break;
		}
	}

	@Override
	public void finish() {
		App.getInstance().getAllActivity().remove(this);
		super.finish();
	}

	@Override
	protected void onDestroy() {
		App.getInstance().getAllActivity().remove(this);
		super.onDestroy();
	}

}
